local UIDebrisChooseList        = UIBase.New(PanelResNames.UIDebrisChooseList)


local fragMgr                   = StrangeFragmentManager

local const_quality             = {SSR = 6, UR = 8, LR = 10, LRPlus = 16}
local m_job_btns                = {}
local chips_btn_frames          = {}
local this                      = UIDebrisChooseList
local cur_list                  = nil
local m_chip_info               = nil

local sort_func = function(a,b)
    if a.quality ~= b.quality then
        return a.quality > b.quality
    else
        if a.level ~= b.level then
            return  a.level > b.level
        end
    end
    return a.itemId > b.itemId
end

local remove_item = function()
    
end

function UIDebrisChooseList.Init(data)
    m_chip_info = data

    UIBase.Init(UIDebrisChooseList)
end

function UIDebrisChooseList:OnCreate(obj)
    self:RegisterOnClick(self:GetChild("Btn_BGClose"), self.OnClick_close)
    self:RegisterOnClick(self:GetChild("btnQuality"), self.OnClick_arrow)
    self:RegisterOnClick(self:GetChild("btn_Return"), self.OnClick_close)

    self.scroll_list    = self:GetChild("content")
    self.btn_SSR        = self:GetChild("btnSSR")
    self.btn_UR         = self:GetChild("btnUR")
    self.btn_LR         = self:GetChild("btnLR")
    self.btn_LRPlus     = self:GetChild("btnLRPlus")
    self.ctr_sort       = self:GetController("sort")
    -- self.ctr_nothing    = self:GetController("nothing")

    self:RegisterOnClick(self.btn_SSR, function()
        self:OnClick_selectQuality(const_quality.SSR)
    end)
    self:RegisterOnClick(self.btn_UR, function()
        self:OnClick_selectQuality(const_quality.UR)
    end)
    self:RegisterOnClick(self.btn_LR, function()
        self:OnClick_selectQuality(const_quality.LR)
    end)
    self:RegisterOnClick(self.btn_LRPlus, function()
        self:OnClick_selectQuality(const_quality.LR)
    end)

    local btn = nil
    m_job_btns = {}
    for i=1, 5 do
        btn = self:GetChild("btn" .. (i - 1))

        btn.onChanged:Add(function()
            this.OnClick_filterJob(i - 1)
        end)
        table.insert(m_job_btns, btn)
    end


    local enum = fragMgr.GetBaptismStatus()
    if enum == Config.Baptism_Step.Select_main_chip then
        cur_list = fragMgr.GetBaptismList(nil)
        table.removeByValue(cur_list, m_chip_info)
    elseif enum == Config.Baptism_Step.Select_sub_chip then
        cur_list = fragMgr.GetBaptismList(m_chip_info)
        table.removeByValue(cur_list, m_chip_info)
    end
    
    table.sort(cur_list, sort_func)
    
    this.scroll_list.itemRenderer = this.SetData
    this.scroll_list.numItems = #cur_list

    chips_btn_frames[6]     = self.btn_SSR
    chips_btn_frames[8]     = self.btn_UR
    chips_btn_frames[10]    = self.btn_LR
    chips_btn_frames[16]    = self.btn_LRPlus
end

local chips_quality_map = {}
function UIDebrisChooseList:OnClick_selectQuality(quality)
    -- if this.m_select_quality == quality then
    --     return
    -- end
    this.m_select_quality = quality

    for k,v in pairs(chips_btn_frames) do
        if k == quality then
            v.selected = true
        else
            v.selected = false
        end
    end

    if this.job_type and this.job_type ~= 0 then
        cur_list = fragMgr.FilterProfession(this.job_type, this.m_select_quality)
    else
        cur_list = fragMgr.FilterQuality(quality, this.job_type)
    end
    
    table.removeByValue(m_chip_info)
    
    this.scroll_list.numItems = #cur_list
end

function UIDebrisChooseList.SetData(index,obj)
    local chip = cur_list[index + 1]
    fragMgr.SetItemData(obj, chip, true)

    obj.onClick:Clear()
    obj.onClick:Add(function()
        FireEvent(Config.EventType.Fragment_Select_Item, cur_list[index + 1])
        closeUI(PanelResNames.UIDebrisChooseList)
    end)
end

function UIDebrisChooseList.OnClick_filterJob(type)
    -- if this.job_type == type then
    --     return
    -- end
    if type == 0 then
        this.m_select_quality = nil
        for k,v in pairs(chips_btn_frames) do
            v.selected = false
        end
    end

    for k,v in pairs(m_job_btns) do
        if k - 1 == type then
            v.selected = true
        else
            v.selected = false
        end
    end
    this.job_type = type

    cur_list = fragMgr.FilterProfession(type, this.m_select_quality)
    table.removeByValue(m_chip_info)
    this.scroll_list.numItems = #cur_list
end

function UIDebrisChooseList:OnClick_arrow()
    self.ctr_sort.selectedIndex = self.ctr_sort.selectedIndex  == 0 and 1 or 0
end

function UIDebrisChooseList:OnClick_close()
    closeUI(PanelResNames.UIDebrisChooseList)
end


return UIDebrisChooseList
